package com.example.myapplication.ui.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.ui.screens.MainScreen
import com.example.myapplication.ui.screens.StorageDetailScreen
import com.example.myapplication.ui.screens.SearchScreen
import com.example.myapplication.ui.screens.ItemDetailScreen

/**
 * 应用导航路由定义
 */
object StorageDestinations {
    const val MAIN_ROUTE = "main"
    const val STORAGE_DETAIL_ROUTE = "storage_detail/{spaceId}"
    const val SEARCH_ROUTE = "search"
    const val ITEM_DETAIL_ROUTE = "item_detail/{itemId}"
    
    fun storageDetailRoute(spaceId: String) = "storage_detail/$spaceId"
    fun itemDetailRoute(itemId: String) = "item_detail/$itemId"
}

/**
 * 应用导航组合函数
 */
@Composable
fun StorageNavigation(
    navController: NavHostController = rememberNavController()
) {
    NavHost(
        navController = navController,
        startDestination = StorageDestinations.MAIN_ROUTE
    ) {
        // 主界面 - 顶级储物空间网格
        composable(StorageDestinations.MAIN_ROUTE) {
            MainScreen(
                onNavigateToStorageDetail = { spaceId ->
                    navController.navigate(StorageDestinations.storageDetailRoute(spaceId))
                },
                onNavigateToSearch = {
                    navController.navigate(StorageDestinations.SEARCH_ROUTE)
                }
            )
        }
        
        // 储物空间详情 - 子空间和物品列表
        composable(StorageDestinations.STORAGE_DETAIL_ROUTE) { backStackEntry ->
            val spaceId = backStackEntry.arguments?.getString("spaceId") ?: return@composable
            StorageDetailScreen(
                spaceId = spaceId,
                onNavigateBack = {
                    navController.popBackStack()
                },
                onNavigateToStorageDetail = { childSpaceId ->
                    navController.navigate(StorageDestinations.storageDetailRoute(childSpaceId))
                },
                onNavigateToItemDetail = { itemId ->
                    navController.navigate(StorageDestinations.itemDetailRoute(itemId))
                },
                onNavigateToSearch = {
                    navController.navigate(StorageDestinations.SEARCH_ROUTE)
                }
            )
        }
        
        // 搜索界面
        composable(StorageDestinations.SEARCH_ROUTE) {
            SearchScreen(
                onNavigateBack = {
                    // 搜索页面直接返回主页而不是上一页
                    navController.popBackStack(StorageDestinations.MAIN_ROUTE, inclusive = false)
                },
                onNavigateToStorageDetail = { spaceId ->
                    navController.navigate(StorageDestinations.storageDetailRoute(spaceId))
                },
                onNavigateToItemDetail = { itemId ->
                    // 从搜索进入物品详情时，添加来源标记
                    navController.navigate("${StorageDestinations.itemDetailRoute(itemId)}?from=search")
                }
            )
        }
        
        // 物品详情界面
        composable("${StorageDestinations.ITEM_DETAIL_ROUTE}?from={from}") { backStackEntry ->
            val itemId = backStackEntry.arguments?.getString("itemId") ?: return@composable
            val from = backStackEntry.arguments?.getString("from")
            ItemDetailScreen(
                itemId = itemId,
                onNavigateBack = {
                    when (from) {
                        "search" -> {
                            // 从搜索进入的，返回搜索页面
                            navController.popBackStack(StorageDestinations.SEARCH_ROUTE, inclusive = false)
                        }
                        else -> {
                            // 其他情况正常返回
                            navController.popBackStack()
                        }
                    }
                },
                onNavigateToStorageDetail = { spaceId ->
                    navController.navigate(StorageDestinations.storageDetailRoute(spaceId))
                }
            )
        }
        
        // 保持原有的物品详情路由兼容性
        composable(StorageDestinations.ITEM_DETAIL_ROUTE) { backStackEntry ->
            val itemId = backStackEntry.arguments?.getString("itemId") ?: return@composable
            ItemDetailScreen(
                itemId = itemId,
                onNavigateBack = {
                    navController.popBackStack()
                },
                onNavigateToStorageDetail = { spaceId ->
                    navController.navigate(StorageDestinations.storageDetailRoute(spaceId))
                }
            )
        }
    }
} 